package com.zmm.myyimixian.ui.activity.main.home;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseFragment;
import com.zmm.myyimixian.bean.HomeBean;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.ui.adapter.MyHomeGvAdapter;
import com.zmm.myyimixian.ui.adapter.MyListOneApater;
import com.zmm.myyimixian.util.UIManager;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener{

    private ImageView mmzc_iv;
    private ImageView ymt_iv;
    private ImageView gdmw_iv;

    public HomeFragment() {

    }

    private MyHandler handler = new MyHandler(this);
    private ConvenientBanner convenientBanner;
    private MyGridView main_gv;
    private MyListView home_one_lv;

    @Override
    public void initView(View view) {
        convenientBanner = (ConvenientBanner) view.findViewById(R.id.convenientBanner);
        main_gv = (MyGridView) view.findViewById(R.id.main_gv);
        home_one_lv = (MyListView) view.findViewById(R.id.home_one_lv);
        view.findViewById(R.id.mmzc_tv).setOnClickListener(this);
        view.findViewById(R.id.ymt_tv).setOnClickListener(this);
        view.findViewById(R.id.gdmw_tv).setOnClickListener(this);
        view.findViewById(R.id.gdsp_tv).setOnClickListener(this);
        mmzc_iv = (ImageView) view.findViewById(R.id.mmzc_iv);
        ymt_iv = (ImageView) view.findViewById(R.id.ymt_iv);
        gdmw_iv = (ImageView) view.findViewById(R.id.gdmw_iv);
        new MyHomeThread(handler).start();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    //实现grad
    private void getGradView(List<HomeBean.CardListBean> cardListBeen) {
        List<HomeBean.CardListBean.MaterialBean> material1 = cardListBeen.get(1).getMaterial();
        MyHomeGvAdapter adapter = new MyHomeGvAdapter(material1, getActivity());
        main_gv.setAdapter(adapter);
        final List<HomeBean.CardListBean.MaterialBean> material2 = cardListBeen.get(2).getMaterial();
        MyListOneApater adapter1 = new MyListOneApater(material2, getActivity());
        home_one_lv.setAdapter(adapter1);
        Glide.with(getActivity()).load(ConfigPath.getLunBoPath("zhuanti/huabei/maizeng_zt_0617.jpg")).placeholder(R.mipmap.icon_home_item_default).into(mmzc_iv);
        Glide.with(getActivity()).load(ConfigPath.getLunBoPath("zhuanti/yimituan.jpg")).placeholder(R.mipmap.icon_home_item_default).into(ymt_iv);
        Glide.with(getActivity()).load(ConfigPath.getLunBoPath("product_v2_fang/beijing/yilianmuxixilasuannai_f1106.jpg")).placeholder(R.mipmap.icon_home_item_default).into(gdmw_iv);
        home_one_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = material2.get(position).getUrl();
                if (position == 0) {
                    UIManager.startGroupPurchase(getActivity(), url);
                }else {
                    UIManager.startDetails(getActivity(), url);
                }
            }
        });

    }


    //实现图片轮播
    private void initData(List<HomeBean.CardListBean> cardListBeen) {
        List<HomeBean.CardListBean.MaterialBean> material = cardListBeen.get(0).getMaterial();
        ArrayList<String> arraylist = new ArrayList<>();
        for (int i = 0; i < material.size(); i++) {
            arraylist.add(material.get(i).getImage());
        }
        convenientBanner.setPages(new CBViewHolderCreator<ViewPagerImage>() {
            @Override
            public ViewPagerImage createHolder() {
                return new ViewPagerImage();
            }
        }, arraylist).setPageIndicator(new int[]{R.mipmap.order_list_orange_ball_invalid, R.mipmap.order_list_orange_ball})
                .setPageIndicatorAlign(
                        ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        getGradView(cardListBeen);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mmzc_tv:
                UIManager.startHomeBoosActivity(getActivity(), "823");
                break;
            case R.id.ymt_tv:
                UIManager.startHomeBoosActivity(getActivity(), "645");
                break;
            case R.id.gdmw_tv:
                UIManager.startHomeBoosActivity(getActivity(), "954");
                break;
            case R.id.gdsp_tv:
                break;
        }
    }


    public class ViewPagerImage implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, String data) {
            Glide.with(getFActivity()).load(ConfigPath.getLunBoPath(data)).placeholder(R.mipmap.icon_home_item_default).into(imageView);
        }
    }

    //重写 onResume onPause 设置自动播放
    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        convenientBanner.startTurning(2000);
    }

    @Override
    public void onPause() {
        super.onPause();
        //停止自动翻页
        convenientBanner.stopTurning();
    }

    //获取网上下载的数据
    private class MyHandler extends Handler {

        private WeakReference<HomeFragment> reference = null;

        public MyHandler(HomeFragment fragment) {
            reference = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            HomeFragment homeFragment = reference.get();
            if (homeFragment != null) {
                if (msg.what == 1) {
                    HomeBean homeBean = (HomeBean) msg.obj;
                    List<HomeBean.CardListBean> card_list = homeBean.getCard_list();
                    initData(card_list);
                }
            }
        }
    }
}
